<?php
/**
 * @file featuresreport.database.inc
 *
 * Ce fichier rassemble tous les hooks 'hook_elements_in_database', qui
 * fournissent un tableau scalaires des éléments dont ils disposent en base,
 * en imitant la manière dont le fichier .info d'une features les conserve.
 * 
 * Seuls les éléments qui vivent UNIQUEMENT en database (pas exportés dans le code)
 * doivent être ramenés.
 * Pour étendre les possibilités de features report, il suffit d'ajouter un hook
 * dans ce fichier ou bien de l'implémenter
 * 
 *  PO pouar exemple, pour le module panels, implémenter 
 * panels_elements_in_database
 * 
 */
 
/**
 * Implements hook_in_database();
 * @see context.module
 */
function context_elements_in_database() {
  
  $version = featuresreport_get_module_version('context');
  $elements_in_database = array();
  
  switch($version) {
    
    case '2' :
      
      $contexts = context_contexts();
      foreach ($contexts as $name => $context) {
        // we don't want default and overriden contexts
        if($context->type == CONTEXT_STORAGE_NORMAL) {
          $elements_in_database[] = $name;
        }
      }
      return $elements_in_database;
      
    case '3' :
      
      // exportables of context 3 are provided by ctools
      // context_load is some kind of wrapper for ctools functions 
      $contexts = context_load();
      foreach ($contexts as $name => $context) {
        // we don't want default and overriden contexts
        if($context->type == 'Normal') {
          $elements_in_database[] = $name;
        }
      }
        
      return $elements_in_database;  
      
  }
  
}
/**
 * Strongarm use CTOOLS with variable table.
 */

function strongarm_elements_in_database() {
  $objects_in_database = array();
  ctools_include('export');
  $objects = ctools_export_load_object('variable');
  foreach($objects as $object) {
    if ($object->type == 'Normal') {
      $objects_in_database[] = $object->name;
    }
  }
  return $objects_in_database;
}

/**
 * Panels use Ctools with page_manager_pages table (what the f*** is that ???)
 */
function panels_elements_in_database() {
  
  $version = featuresreport_get_module_version('context');
  if ($version < 3) {
    $message = t('It seems that you have not installed panels 3, but a previous version of panels. Features report does not support panels 2 and previsous version for now.');
    drupal_set_message($message, 'error');
    return;
  }
  
  if ($version >= 3) {
    $objects_in_database = array();
    ctools_include('export');
    $objects = ctools_export_load_object('page_manager_pages');
    foreach($objects as $object) {
      if ($object->type == 'Normal') {
        $objects_in_database[] = $object->name;
      }
    }
    return $objects_in_database;
  }

  $elements_in_database = array();
  
}

  
/**
 * Retourne cck fields. fields can only live in database anyway.
 * Implements hook_in_database();
 * @see content.moduleramerais 
 */
function content_elements_in_database() {
  $elements_in_base = array();
  foreach(content_types() as $type_name => $datas) {
    foreach ($datas['fields'] as $field_name => $datas) {
      // dans les .infos des features, les champs cck contiennent à la fois le nom du champ et du type de contenu
      // on formate notre tableau de la même façon pour faire le diff facilement.
      $elements_in_base[] = $type_name. '-' . $field_name;
    }      
  }
  return $elements_in_base;
}
  
/**
 * implements hook_in_database()
 * @see imagecache.module
 */
function imagecache_elements_in_database() {
  $elements_in_database = array();
  foreach (imagecache_presets() as $preset) {
    if($preset['storage'] == IMAGECACHE_STORAGE_NORMAL) {
      $elements_in_database[] = $preset['presetname'];
    }
  }
  return $elements_in_database;
}
  
/**
 * implements component_in_database()
 * @see fieldgroup.module
 */
function fieldgroup_elements_in_database() {
  $elements_in_database = array();
  foreach(content_types() as $content_type => $datas) {
    foreach (fieldgroup_groups($content_type) as $fieldgroup_name => $datas) {
      $elements_in_database[] = $content_type . '-' . $fieldgroup_name;
    }
  }
  return $elements_in_database;
}
  
/**
 * implements hook_in_database()
 * @see views.module
 */
function views_elements_in_database($reset = FALSE) {
  $elements_in_database = array();
  if ($views = views_get_all_views()) {
    foreach ($views as $view)  {
      if ($view->type == 'Normal') {
        $elements_in_database[] = $view->name;
      }
    }
  }
  return $elements_in_database;
}

/**
 * Content types can be exported twice without side effects
 * So we list all of them and see wich one are already in a features
 */
function node_elements_in_database() {
  $elements_in_database = array();  
  $type_result = db_query('SELECT nt.type FROM {node_type} nt ORDER BY nt.type ASC');
  // features seems too only add to features.info in "node" elements
  // content types that are created via the admin of content types.
  // they are tagged as "node" in database.
  // @todo check this...
  while ($type_object = db_fetch_object($type_result)) {
    if ($type_object == 'node') {
      $elements_in_database[] = $type_object->type;
    }
  }
  return $elements_in_database;
}
